package com.erp.controller;

import com.erp.Utils.ResponseModel;
import com.erp.entity.MyRole;
import com.erp.entityVo.MenuALLVo;
import com.erp.entityVo.RoleDto;
import com.erp.service.MyRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2021-10-12
 */
@Controller
@RequestMapping("/api/role")
@Api(tags = "系统：角色管理")
public class MyRoleController {
    @Autowired
    MyRoleService myRoleService;

    @GetMapping("/index")
    @PreAuthorize("hasAnyAuthority('role:list')")
    public String index(){
        return "system/role/role";
    }

    @ResponseBody
    @GetMapping("/list")
    @ApiOperation("获取角色列表")
    @PreAuthorize("hasAnyAuthority('role:list')")
    public Map<String,Object> userList(@RequestParam(value = "realName",required = false) String realName,
                                       @RequestParam(value = "page") int page,
                                       @RequestParam(value = "limit") int limit){
        return myRoleService.roleList(realName,page,limit);
    }


    @GetMapping("/roleAdd")
    @ApiOperation("角色添加页面")
    @PreAuthorize("hasAnyAuthority('role:add')")
    public ModelAndView roleAdd(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("MyRole",new MyRole());
        modelAndView.setViewName("system/role/role-add");
        return modelAndView;
    }
    @PostMapping("/roleAdd")
    @ApiOperation("角色添加数据")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('role:add')")
    public ResponseModel roleAdd(@RequestBody RoleDto roleDto){
        return myRoleService.save(roleDto);
    }

    @GetMapping("build")
    @ResponseBody
    @ApiOperation(value = "绘制菜单树")
    public ResponseModel buildMenuAll(){
        List<MenuALLVo> menuAll = myRoleService.buildMenuAll();
        return ResponseModel.success(menuAll);
    }

    @GetMapping("roleEdit")
    @ApiOperation(value = "修改角色页面")
    @PreAuthorize("hasAnyAuthority('role:edit')")
    public ModelAndView roleEdit(@RequestParam int roleId){
        Map<String,Object> myRole = myRoleService.getColeInfo(roleId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("system/role/role-edit");
        modelAndView.addAllObjects(myRole);
        return modelAndView;
    }
    @PostMapping("roleEdit")
    @ApiOperation(value = "修改角色信息")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('role:edit')")
    public ResponseModel roleEdit(@RequestBody RoleDto roleDto){
        return myRoleService.update(roleDto);
    }

    @GetMapping("/ebuild")
    @ResponseBody
    @ApiOperation(value = "通过id绘制菜单树")
    public ResponseModel buildMenuAllByRoleId(@RequestParam Integer roleId){
        List<MenuALLVo> menuAll = myRoleService.buildMenuAllByRoleId(roleId);
        return ResponseModel.success(menuAll);
    }

    @DeleteMapping("/remove/{roleId}")
    @ResponseBody
    @ApiOperation("删除角色信息")
    @PreAuthorize("hasAnyAuthority('role:del')")
    public ResponseModel remove(@PathVariable("roleId") String roleId){
        return myRoleService.remove(roleId);
    }

    @GetMapping("/all")
    @ResponseBody
    @ApiOperation("获取所有角色列表")
    @PreAuthorize("hasAnyAuthority('role:del')")
    public ResponseModel all(){
        return myRoleService.all();
    }

}
